import native from '../../../native.js'
import { error } from '../../../../lib/output.js'

const run = (runtime, ast, node) => {
  if (node.type !== 'FunctionDeclaration') return

  const fun = node.body.body
  if (node.body.type === 'NativeFunctionBody') {
    if (native[fun] === undefined)
      error(`Native function ${ fun } is not defined`)
  }

  runtime.methods.set(runtime.alias === null || runtime.alias.length === 0 ? node.id.name : `${ runtime.alias }.${ node.id.name }`, {
    params: node.params,
    body: node.body.type === 'FunctionBody' ? fun : native[fun]
  })
}

export default run
